﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Boiling.DataAccess
{
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Reflection;
    using System.Text;

    using Boiling.Common;

    public class b_Friends : IDisposable
    {
        private SqlDataAdapter dscommand;

        public b_Friends()
        {
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(true);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!disposing)
                return;
            if (dscommand != null)
            {
                if (dscommand.SelectCommand != null)
                {
                    if (dscommand.SelectCommand.Connection != null)
                        dscommand.SelectCommand.Connection.Dispose();
                    dscommand.SelectCommand.Dispose();
                }
                dscommand.Dispose();
                dscommand = null;
            }
        }
        //
        //指令资源分配方式的代码段，与逻辑无关
        //

        public b_Friendsdata getfriendsbyID(string username)
        {
            //用用户注册邮箱取得一个用户好友的列表
            b_Friendsdata newdata = new b_Friendsdata();
            string selectstring = "SELECT * FROM b_Friends WHERE Host_User_ID =" + "'" + username + "' AND Request  = 2";
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();
            dscommand.Fill(newdata, "b_Friends");
            tempconn.Close();
            return (newdata);
        }

        public b_Friendsdata getfriendnotbyID(string username)
        {
            //用户注册邮箱取得未确认的用户好友的列表
            b_Friendsdata newdata = new b_Friendsdata();
            string selectstring = "SELECT * FROM b_Friends WHERE Host_User_ID =" + "'" + username + "' AND Request  = 1";
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();
            dscommand.Fill(newdata, "b_Friends");
            tempconn.Close();
            return (newdata);
        }

        public int countfriendbyID(string username)
        {
            //计算某人一共有多少好友，要求输入其注册邮箱
            b_Friendsdata newdata = getfriendsbyID(username);
            return (newdata.Tables["b_Friends"].Rows.Count);
        }

        public bool delete( string username1,string username2)
        {
            //删除一个好友关系，要求输入两个人的注册邮箱
            //注意两个人之间的好友关系删除需要调用此函数两次，参数调换
            b_UserAddInfodata newdata = new b_UserAddInfodata();
            string selectstring = "SELECT * FROM b_Friends WHERE Host_User_ID =" + "'" + username1 + "'" + " AND Guest_User_ID ='" + username2 + "'";
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommandBuilder da = new SqlCommandBuilder(dscommand);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();

            dscommand.Fill(newdata, "b_Friends");

            if (newdata.Tables["b_Friends"].Rows.Count == 0)
                return (false);
            else
            {
                newdata.Tables["b_Friends"].Rows[0].Delete();
                dscommand.Update(newdata, "b_Friends");
                tempconn.Close();
                return (true);
            }
        }
        public bool deleteFriends(string username1, string username2)
        {
            //调用delete函数两次，删除两个人之间的好友关系
            //提供给FACADE的最终删除函数
            delete(username1, username2);
            return (delete(username2, username1));
        }

        public bool iffriend(string username1, string username2)
        {
            //检查两个用户是否已经是确认好友
            //是则返回true，否则返回false
            b_Friendsdata newdata = new b_Friendsdata();
            string selectstring = "SELECT * FROM b_Friends WHERE Host_User_ID =" + "'" + username1 + "'" + " AND Guest_User_ID ='" + username2 + "'";
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();
            dscommand.Fill(newdata, "b_Friends");
            tempconn.Close();
            if (newdata.Tables["b_Friends"].Rows.Count == 0)
                return false;
            else if (Int32.Parse(newdata.Tables["b_Friends"].Rows[0]["Request"].ToString()) != 2)
                return false;
            else
                return true;
            
               
        }

        public bool iffriend1(string username1, string username2)
        {
            //检查两个用户是否是待确认的好友关系
            //是则返回true，否则返回false
            b_Friendsdata newdata = new b_Friendsdata();
            string selectstring = "SELECT * FROM b_Friends WHERE Host_User_ID =" + "'" + username1 + "'" + " AND Guest_User_ID ='" + username2 + "'";
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();
            dscommand.Fill(newdata, "b_Friends");
            tempconn.Close();
            if (newdata.Tables["b_Friends"].Rows.Count == 0)
                return false;
            else if (Int32.Parse(newdata.Tables["b_Friends"].Rows[0]["Request"].ToString()) == 2)
                return false;
            else
                return true;
        }

    
       

        public int getfrienddeg(string username1, string username2)
        {
            //输入两个用户的注册邮箱，返回其亲密度
            //如果两个用户之间没有好友关系，返回-1
            //否则返回数据库中两个用户间的亲密程度
            if (!iffriend(username1, username2))
                return (-1);
            else
            {

                b_Friendsdata newdata = new b_Friendsdata();
                string selectstring = "SELECT * FROM b_Friends WHERE Host_User_ID =" + "'" + username1 + "'" + " AND Guest_User_ID ='" + username2 + "'";
                dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
                SqlCommand selcmd = dscommand.SelectCommand;
                SqlConnection tempconn = selcmd.Connection;
                tempconn.Open();
                dscommand.Fill(newdata, "b_Friends");
                tempconn.Close();
                return (Int32.Parse( newdata.Tables["b_Friends"].Rows[0]["Friend_Deg"].ToString ()));
            }
        }

        public bool incfriend(string username1, string username2)
        {
            //输入两个用户的注册邮箱，增加两个用户的亲密程度
            //注意对于两个用户而言，需要调用这个函数两次,参数调换
            //如果不存在好友关系则返回false,否则返回true
            b_UserAddInfodata newdata = new b_UserAddInfodata();
            string selectstring = "SELECT * FROM b_Friends WHERE Host_User_ID =" + "'" + username1 + "'" + " AND Guest_User_ID ='" + username2 + "'";
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommandBuilder da = new SqlCommandBuilder(dscommand);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();

            dscommand.Fill(newdata, "b_Friends");

            if (newdata.Tables["b_Friends"].Rows.Count == 0)
                return (false);
            else
            {
                int i =Int32.Parse ( newdata.Tables["b_Friends"].Rows[0]["Friends_Deg"].ToString ());
                i++;
                newdata.Tables["b_Friends"].Rows[0]["Friends_Deg"] = i;
                dscommand.Update(newdata, "b_Friends");
                tempconn.Close();
                return (true);
            }

        }
        public bool incfrienddeg(string username1, string username2)
        {
            //提供给逻辑层的增加两个好友之间亲密程度的函数
            //调用incfriend函数两次
            incfriend(username1, username2);
            return(incfriend (username2,username1));
        }

        public bool insert0(string username1, string username2)
        {
            //向数据库插入一个好友关系，要求输入两个人的用户注册邮箱
            //默认两个人的亲密程度为0
            //插入时要求尚未满足,Request 为0（发起者）
            string insertstring = "INSERT b_Friends (Host_User_ID,Guest_User_ID,Friends_Deg,Request) VALUES('" + username1 + "','" + username2 + "',0,0)";
            SqlConnection cn = new SqlConnection(BoilingConfiguration.dbConnnectionstring);
            SqlCommand cmd = new SqlCommand(insertstring, cn);
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
            return true;
        }
        public bool insert1(string username1, string username2)
        {
            //向数据库插入一个好友关系，要求输入两个人的用户注册邮箱
            //默认两个人的亲密程度为0
            //插入时要求尚未满足,Request 为1（接受者）
            string insertstring = "INSERT b_Friends (Host_User_ID,Guest_User_ID,Friends_Deg,Request) VALUES('" + username1 + "','" + username2 + "',0,1)";
            SqlConnection cn = new SqlConnection(BoilingConfiguration.dbConnnectionstring);
            SqlCommand cmd = new SqlCommand(insertstring, cn);
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
            return (true);

        }
        public bool insertFrined(string username1, string username2)
        {
            //提供给逻辑层的最终添加函数
            insert0(username1, username2);
            insert1(username2, username1);
            return true;

        }
        public bool confirm(string username1, string username2)
        {
            //确认一个发起的好友关系
            //完整确认过程需要调用该函数两次
            b_UserAddInfodata newdata = new b_UserAddInfodata();
            string selectstring = "SELECT * FROM b_Friends WHERE Host_User_ID =" + "'" + username1 + "'" + " AND Guest_User_ID ='" + username2 + "'";
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommandBuilder da = new SqlCommandBuilder(dscommand);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();

            dscommand.Fill(newdata, "b_Friends");

            if (newdata.Tables["b_Friends"].Rows.Count == 0)
                return (false);
            else
            {
                newdata.Tables["b_Friends"].Rows[0]["Request"] =2;
                dscommand.Update(newdata, "b_Friends");
                tempconn.Close();
                return (true);
            }
        }
        public bool confirmFriend(string usernam1, string username2)
        {
            //提供给逻辑层的确认好友关系的函数
            //要求输入两个人的用户注册邮箱
            confirm(usernam1, username2);
            return (confirm(username2, usernam1));
        }


    }
}
